<template>
	<div class="subpage addresslist">
		<van-address-list
		  v-model="chosenAddressId"
		  :list="list"
		  @add="onAdd"
		  @edit="onEdit"
		  @select="itemClick"
		/>
	</div>
</template>

<script>
	import Vue from 'vue';
	import { AddressList } from 'vant';
	Vue.use(AddressList);
	import Storage from "@/utils/storage";
	export default {
		data() {
			return {
				id: '',
				chosenAddressId: null,
				list: [],
				updateAddress: {
					
				},
				busData: {
					from: '',
					orderId: ''
				}
			}
		},
		created(){
			this.id = this.$route.params.id;
			// 地址列表数据
			this.getAddressList()
			
			//  从 采购订单详情进入修改地址会 传 from 和 orderId  作标识符
			if(Object.keys(this.$route.query).length !== 0) {
				Storage.set('from', this.$route.query.from);
				Storage.set('orderId', this.$route.query.orderId)
				Storage.set('orderDetailId', this.$route.query.orderDetailId)
			}
			
		},
		methods: {
			// 地址列表数据
			getAddressList(){
				this.$api.post('/api/user/getaddresslist')
				.then(({data})=>{
					console.log(data)
					if(data.length > 0){
						this.list = data.map(e=>{
							e.address = e.province + e.city + e.county + e.addressDetail
							return e
						})
						this.chosenAddressId = data[0].id;   // 默认第一条打勾
					} else {
						Storage.del('addressId')
					}
					
				})
			},
			// 选中的 item点击
			itemClick(value){
				this.list.map(item=>{
					if(value.id == item.id){  // 根据id 调接口 更新地址  点击之后排在第一行
						delete item.updateTime;
						Storage.set('addressId', item.id);
						if(Storage.get('from') == 'purchaseOrderDetail'){   // 采购订单详情  修改地址
							item.orderId = Storage.get('orderDetailId')  // 添加订单id
							this.$api.post('/api/order/edit_address/' + item.orderId, item)
							.then((res)=>{
								console.log(res)
								this.$router.back()
							})
						} else if(Storage.get('from') == 'tab2detail' || Storage.get('from') == 'helppay'){   //  我的模块 头像点进去  收货地址 选择完地址返回
							this.$api.post('/api/user/updateaddress', item)
							.then((res)=>{
								console.log(res);
								this.$router.back()
							})
						} else if(Storage.get('from') == 'personalInfo'){   //   个人中心
							item.isDefault = 1;
							this.$api.post('/api/user/updateaddress', item)
							.then((res)=>{
								console.log(res);
								this.$router.back()
							})
						} else {
							this.$api.post('/api/user/updateaddress', item)
							.then((res)=>{
								console.log(res)
							})
							this.$router.back()
						}
					}
				})
			},
			// 增加
			onAdd() {
				this.$router.push({
					path: '/home/address/' + this.id
				})
		    },
			// 修改
		    onEdit(item, index) {
				delete item.updateTime
				let newitem = JSON.stringify(item);
				this.$router.push({
					path: '/home/address/' + this.id,
					query:{ item: newitem }
				})
		    },
			
	  },
	  beforeDestroy(){
		  // 因为个人中心 和 采购订单详情有地址 跳到这个页面修改 或 添加 from只是作为标识符
		if(this.$route.path !== '/home/address/' + this.id){
			Storage.del('from');
			Storage.del('orderId');
			Storage.del('orderDetailId')
		}
	  }
	}
	

</script>

<style lang="scss" scoped>
.van-address-list{
	height: auto !important;
}
.addresslist{
	background-color: #fff;
}
.van-address-list__add{
	bottom: 12px !important;
	width: 343px;
	height:51px;
	background:linear-gradient(94deg,rgba(49,188,141,1) 0%,rgba(166,226,155,1) 100%);
	border-radius:26px;
	text-align: center;
	font-size: 18px;
	border: none;
	left: 50%;
	margin-left: -170px;
	box-shadow: 0 6px 10px #19884f66;
}
</style>